package com.yserp.project.erp.order.sampleregistration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yserp.common.constant.Constants;
import com.yserp.common.exception.BusinessException;
import com.yserp.common.utils.mybatis.MybatisPlusUtil;
import com.yserp.common.utils.text.Convert;
import com.yserp.project.erp.basic.customer.domain.BasisCustomer;
import com.yserp.project.erp.basic.customer.service.IBasisCustomerService;
import com.yserp.project.erp.common.enums.OrderEnums;
import com.yserp.project.erp.order.sampleregistration.domain.SampleRigistration;
import com.yserp.project.erp.order.sampleregistration.mapper.SampleRigistrationMapper;
import com.yserp.project.erp.order.sampleregistration.service.ISampleRigistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 样品登记Service业务层处理
 *
 * @author yserp
 * @date 2022-08-01
 */
@Service
public class SampleRigistrationServiceImpl extends ServiceImpl<SampleRigistrationMapper, SampleRigistration> implements ISampleRigistrationService {

    @Resource
    private SampleRigistrationMapper sampleRigistrationMapper;
    @Autowired
    private IBasisCustomerService customerService;

    //同步状态值
    private static final String TransFlag="1";

    /**
     * 查询样品登记
     *
     * @param id 样品登记主键
     * @return 样品登记
     */
    @Override
    public SampleRigistration selectSampleRigistrationById(Long id) {
        return sampleRigistrationMapper.selectById(id);
    }

    /**
     * 查询样品登记列表
     *
     * @param sampleRigistration 样品登记
     * @return 样品登记
     */
    @Override
    public List<SampleRigistration> selectSampleRigistrationList(SampleRigistration sampleRigistration) {
        QueryWrapper<SampleRigistration> query = new QueryWrapper<>();
        MybatisPlusUtil.notNullField(sampleRigistration,query);
        return sampleRigistrationMapper.selectList(query);
    }

    /**
     * 新增样品登记
     *
     * @param sampleRigistration 样品登记
     * @return 结果
     */
    @Override
    public int insertSampleRigistration(SampleRigistration sampleRigistration) {
        return sampleRigistrationMapper.insert(sampleRigistration);
    }

    /**
     * 修改样品登记
     *
     * @param sampleRigistration 样品登记
     * @return 结果
     */
    @Override
    public int updateSampleRigistration(SampleRigistration sampleRigistration) {
        return sampleRigistrationMapper.updateById(sampleRigistration);
    }

    /**
     * 批量删除样品登记
     *
     * @param ids 需要删除的样品登记主键
     * @return 结果
     */
    @Override
    public int deleteSampleRigistrationByIds(String ids) {
        return sampleRigistrationMapper.deleteBatchIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 删除样品登记信息
     *
     * @param id 样品登记主键
     * @return 结果
     */
    @Override
    public int deleteSampleRigistrationById(Long id) {
        return sampleRigistrationMapper.deleteById(id);
    }

    @Override
    public Boolean transCustomer(Long id) {
        //1.获取详情
        SampleRigistration sampleRigistration = this.getById(id);
        if(null == sampleRigistration) {
            throw new BusinessException("数据不存在");
        }
        //2.是否同步
        String transFlag = sampleRigistration.getTransFlag();
        if(TransFlag.equals(transFlag)){
            throw new BusinessException("已同步，无须重复同步");
        }
        //3.查询客户是否存在
        BasisCustomer customer = BasisCustomer.builder().customerName(sampleRigistration.getCompanyName())
                .contactPerson(sampleRigistration.getContactPerson())
                .telephone(sampleRigistration.getPhone())
                .address(sampleRigistration.getAddress()).build();
        List<BasisCustomer> basisCustomers = customerService.selectBasisCustomerList(customer);
        if(!CollectionUtils.isEmpty(basisCustomers)){
            throw new BusinessException("客户信息已存在");
        }
        //4.插入数据
        customer.setCustomerCode(getCustomerOrderNo());
        customerService.insertBasisCustomer(customer);
        //5.更新状态
        SampleRigistration sample = new SampleRigistration();
        sample.setId(id);
        sample.setTransFlag(TransFlag);
        return this.updateById(sample);
    }


    private String getCustomerOrderNo(){
        String lowOrderNo = OrderEnums.getLowOrderNo(OrderEnums.CUSTOMER_DOCUMENT.getPref());
        String flag = customerService.checkCustomerCodeUnique(lowOrderNo);
        if(Constants.NAME_NOT_UNIQUE.equals(flag)){
            lowOrderNo = getCustomerOrderNo();
        }
        return lowOrderNo;
    }
}
